package sdc.media.motorbike.Controller;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Assets {

	// Load 1 time...
	public static boolean isload = false;
	
	// Texture: Menu Screen, Button, etc...
	public static TextureRegion backgroundMenu;
	public static TextureRegion logo;
	public static TextureRegion car1;
	public static TextureRegion car2;
	public static TextureRegion car3;
	public static TextureRegion enemy1;
	public static TextureRegion enemy2;
	public static TextureRegion enemy3;
	public static TextureRegion tree;
	public static TextureRegion map_1;
	public static TextureRegion map_2;
	public static TextureRegion map_3;
	public static TextureRegion den_duong;
	public static TextureRegion den_duong_flip;
	public static TextureRegion den_ham;
	public static TextureRegion den_oto;
	public static TextureRegion den_oto_2;
	public static TextureRegion xi_nhan;
	public static TextureRegion xi_nhan_2;
	
	// Play
	public static TextureRegion buttonPlay;
	public static TextureRegion buttonSelectMotor;
	public static TextureRegion buttonHighScore;
	public static TextureRegion buttonGuide;
	public static TextureRegion buttonExit;
	public static TextureRegion buttonPlay_click;
	public static TextureRegion buttonSelectMotor_click;
	public static TextureRegion buttonHighScore_click;
	public static TextureRegion buttonGuide_click;
	public static TextureRegion buttonExit_click;
	public static TextureRegion buttonSpeed;
	public static TextureRegion kim_dong_ho;
	public static TextureRegion pause;
	public static TextureRegion resume;
	public static TextureRegion quit;
	public static TextureRegion speedImage;
	public static TextureRegion scoreImage;
	public static TextureRegion pause_click;
	public static TextureRegion resume_click;
	public static TextureRegion quit_click;
	public static TextureRegion checkQuit;
	public static TextureRegion yes;
	public static TextureRegion no;
	public static TextureRegion pauseImage;
	public static TextureRegion readyImage;
	public static TextureRegion gameoverImage;
	
	// Select Screen
	public static TextureRegion selectBackground;
	public static TextureRegion select_1;
	public static TextureRegion select_2;
	public static TextureRegion select_3;
	public static TextureRegion locked;
	public static TextureRegion left;
	public static TextureRegion left_click;
	public static TextureRegion right;
	public static TextureRegion right_click;
	
	public static TextureRegion highScore;
	public static TextureRegion guide;

	// Font
	public static BitmapFont font_1;
	public static BitmapFont font_2;

	// Animation
	//public static TextureRegion[] exampleTexture;
	//public static Animation exampleAnimation;
	public static TextureRegion[] explore;
	public static com.badlogic.gdx.graphics.g2d.Animation exploreAnimation;

	// Music and sound
	public static Music backgroundPlayMusic;
	public static Sound buttonClick;
	public static Music dichuyen;
	public static Music tangtoc;
	public static Sound vacham;
	

	public static void load() {
		
	/**********************************************************************************************/
		// Texture: Menu Screen, Button, etc...
		backgroundMenu = loadTextureRegion("data/image/texture/backgroundMenu.png");
		logo = loadTextureRegion("data/image/texture/logo.png");
		car1 = loadTextureRegion("data/image/texture/oto1.png");
		car2 = loadTextureRegion("data/image/texture/oto2.png");
		car3 = loadTextureRegion("data/image/texture/oto3.png");
		enemy1 = loadTextureRegion("data/image/texture/vatcan1.png");
		enemy2 = loadTextureRegion("data/image/texture/vatcan2.png");
		enemy3 = loadTextureRegion("data/image/texture/vatcan3.png");
		tree = loadTextureRegion("data/image/texture/tree.png");
		map_1 = loadTextureRegion("data/image/texture/backgroundPlay_1.png");
		map_2 = loadTextureRegion("data/image/texture/backgroundPlay_2.png");
		map_3 = loadTextureRegion("data/image/texture/backgroundPlay_3.png");
		den_duong = loadTextureRegion("data/image/texture/den.png");
		den_duong_flip = loadTextureRegion("data/image/texture/den.png");
		den_duong_flip.flip(true, false);
		den_ham = loadTextureRegion("data/image/texture/den-ham.png");
		den_oto = loadTextureRegion("data/image/texture/den_oto.png");
		den_oto_2 = loadTextureRegion("data/image/texture/den_oto_2.png");
		xi_nhan = loadTextureRegion("data/image/texture/xi_nhan.png");
		xi_nhan_2 = loadTextureRegion("data/image/texture/xi_nhan_2.png");
		
		// Play
		buttonPlay = loadTextureRegion("data/image/texture/play/play.png");
		buttonSelectMotor = loadTextureRegion("data/image/texture/play/select_motor.png");
		buttonHighScore = loadTextureRegion("data/image/texture/play/highscore.png");
		buttonGuide = loadTextureRegion("data/image/texture/play/guide.png");
		buttonExit = loadTextureRegion("data/image/texture/play/exit.png");
		buttonPlay_click = loadTextureRegion("data/image/texture/play/play_click.png");
		buttonSelectMotor_click = loadTextureRegion("data/image/texture/play/select_motor_click.png");
		buttonHighScore_click = loadTextureRegion("data/image/texture/play/highscore_click.png");
		buttonGuide_click = loadTextureRegion("data/image/texture/play/guide_click.png");
		buttonExit_click = loadTextureRegion("data/image/texture/play/exit_click.png");
		buttonSpeed = loadTextureRegion("data/image/texture/dongho.png");
		kim_dong_ho = loadTextureRegion("data/image/texture/kim-dh.png");
		pause = loadTextureRegion("data/image/texture/pause.png");
		resume = loadTextureRegion("data/image/texture/resume.png");
		quit = loadTextureRegion("data/image/texture/quit.png");
		speedImage = loadTextureRegion("data/image/texture/speed.png");
		scoreImage = loadTextureRegion("data/image/texture/score.png");
		pause_click = loadTextureRegion("data/image/texture/pause_click.png");
		resume_click = loadTextureRegion("data/image/texture/resume_click.png");
		quit_click = loadTextureRegion("data/image/texture/quit_click.png");
		checkQuit = loadTextureRegion("data/image/texture/check_quit.png");
		yes = loadTextureRegion("data/image/texture/yes.png");
		no = loadTextureRegion("data/image/texture/no.png");
		pauseImage = loadTextureRegion("data/image/texture/pause_image.png");
		readyImage = loadTextureRegion("data/image/texture/ready_image.png");
		gameoverImage = loadTextureRegion("data/image/texture/gameover_image.png");
		
		// Select Screen
		selectBackground = loadTextureRegion("data/image/texture/backgroundSelect.png");
		select_1 = loadTextureRegion("data/image/texture/select1.png");
		select_2 = loadTextureRegion("data/image/texture/select2.png");
		select_3 = loadTextureRegion("data/image/texture/select3.png");
		locked = loadTextureRegion("data/image/texture/lock.png");
		left = loadTextureRegion("data/image/texture/next.png");
		left.flip(true, false);
		right = loadTextureRegion("data/image/texture/next.png");
		left_click = loadTextureRegion("data/image/texture/next_click.png");
		left_click.flip(true, false);
		right_click = loadTextureRegion("data/image/texture/next_click.png");
		
		highScore = loadTextureRegion("data/image/texture/highscore.png");
		guide = loadTextureRegion("data/image/texture/guide.png");
		
	/**********************************************************************************************/
		// Font
		font_1 = new BitmapFont(Gdx.files.internal("data/uiskin/arial.fnt"),
				Gdx.files.internal("data/uiskin/arial.png"), false);
		font_2 = new BitmapFont(Gdx.files.internal("data/uiskin/comic.fnt"),
				Gdx.files.internal("data/uiskin/comic.png"), false);
		
	/**********************************************************************************************/
		// Animation
		//exampleTexture = load_arrayTexture("data/image/animation/example", 8);
		//exampleAnimation = new Animation(0.1f, false, exampleTexture);
		explore = loadArrayTexture(5, 1, "data/image/texture/explore.png");
		exploreAnimation = new com.badlogic.gdx.graphics.g2d.Animation(0.1f, explore);
		
	/**********************************************************************************************/
		// Music and sound
		// Music
		backgroundPlayMusic = Gdx.audio.newMusic(Gdx.files
				.internal("data/music/backgroundPlay.wav"));
		backgroundPlayMusic.setLooping(true);
		
		dichuyen = Gdx.audio.newMusic(Gdx.files
				.internal("data/music/dichuyenbandau.ogg"));
		dichuyen.setLooping(true);
		
		tangtoc = Gdx.audio.newMusic(Gdx.files
				.internal("data/music/tangtoc.ogg"));
		tangtoc.setLooping(true);
		
	/**********************************************************************************************/
		// Sound
		buttonClick = Gdx.audio.newSound(Gdx.files
				.internal("data/music/buttonClick.wav"));
		
		vacham = Gdx.audio.newSound(Gdx.files
				.internal("data/music/damnhau.ogg"));
		
		// make sure load asset 1 time only
		isload = true;
		
	/**********************************************************************************************/
	}
	
	// Load texture
	public static Texture loadTexture(String file) {
		return new Texture(Gdx.files.internal(file));
	}

	// Load texture region
	public static TextureRegion loadTextureRegion(String path) {
		Texture.setEnforcePotImages(false);
		return new TextureRegion(new Texture(Gdx.files.internal(path)));
	}

	// Load animation from file
	public static TextureRegion[] load_arrayTexture(String str, int count) {
		TextureRegion[] arrayTextureRegion = new TextureRegion[count];
		for (int i = 0; i < count; i++) {
			arrayTextureRegion[i] = loadTextureRegion(str +"/"+ (i + 1) + ".png");
		}
		return arrayTextureRegion;
	}

	public static TextureRegion[] loadArrayTexture(int col, int row,
			String direction) {
		TextureRegion[] textureRegion;

		Texture texture_ball = new Texture(Gdx.files.internal(direction));
		TextureRegion[][] tmp = TextureRegion.split(texture_ball,
				texture_ball.getWidth() / col, texture_ball.getHeight() / row);
		textureRegion = new TextureRegion[col * row];

		int index = 0;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				textureRegion[index++] = tmp[i][j];
			}
		}

		return textureRegion;

	}
	
	// Play Sound
	public static void playSound(Sound sound) {
		if (Setting.soundEnabled) {
			sound.play(1);
		}
	}

	// Play Music
	public static void playMusic(Music music) {
		if (Setting.soundEnabled) {
			music.play();
		}
	}
	
	public static void dispose() {
		// Texture: Menu Screen, Button, etc...
		backgroundMenu.getTexture().dispose();
		logo.getTexture().dispose();
		car1.getTexture().dispose();
		car2.getTexture().dispose();
		car3.getTexture().dispose();
		enemy1.getTexture().dispose();
		enemy2.getTexture().dispose();
		enemy3.getTexture().dispose();
		tree.getTexture().dispose();
		map_1.getTexture().dispose();
		map_2.getTexture().dispose();
		map_3.getTexture().dispose();
		den_duong.getTexture().dispose();
		den_duong_flip.getTexture().dispose();
		den_ham.getTexture().dispose();
		den_oto.getTexture().dispose();
		den_oto_2.getTexture().dispose();
		xi_nhan.getTexture().dispose();
		xi_nhan_2.getTexture().dispose();
		
		buttonPlay.getTexture().dispose();
		buttonSelectMotor.getTexture().dispose();
		buttonHighScore.getTexture().dispose();
		buttonGuide.getTexture().dispose();
		buttonExit.getTexture().dispose();
		buttonPlay_click.getTexture().dispose();
		buttonSelectMotor_click.getTexture().dispose();
		buttonHighScore_click.getTexture().dispose();
		buttonGuide_click.getTexture().dispose();
		buttonExit_click.getTexture().dispose();
		buttonSpeed.getTexture().dispose();
		kim_dong_ho.getTexture().dispose();
		pause.getTexture().dispose();
		resume.getTexture().dispose();
		quit.getTexture().dispose();
		speedImage.getTexture().dispose();
		scoreImage.getTexture().dispose();
		pause_click.getTexture().dispose();
		resume_click.getTexture().dispose();
		quit_click.getTexture().dispose();
		checkQuit.getTexture().dispose();
		yes.getTexture().dispose();
		no.getTexture().dispose();
		pauseImage.getTexture().dispose();
		readyImage.getTexture().dispose();
		gameoverImage.getTexture().dispose();
		
		selectBackground.getTexture().dispose();
		select_1.getTexture().dispose();
		select_2.getTexture().dispose();
		select_3.getTexture().dispose();
		locked.getTexture().dispose();
		left.getTexture().dispose();
		left_click.getTexture().dispose();
		right.getTexture().dispose();
		right_click.getTexture().dispose();
		
		highScore.getTexture().dispose();
		guide.getTexture().dispose();
		
		// Font
		font_1.dispose();
		font_2.dispose();

		// Animation
		for (int i = 0; i < explore.length; i++) {
			explore[i].getTexture().dispose();
		}
		exploreAnimation = null;

		// Music and sound
		backgroundPlayMusic.dispose();
		buttonClick.dispose();
		tangtoc.dispose();
		dichuyen.dispose();
		vacham.dispose();
	}
	
}
